﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Tdb.YY.DTO;
using Tdb.YY.Helper;
using Tdb.YY.Model;

namespace Tdb.YY.EncryptTool
{
    class Program
    {
        static void Main(string[] args)
        {
            Parallel.For(1, 19, new ParallelOptions() { MaxDegreeOfParallelism = 4 }, (index) =>
            {
                Console.WriteLine($"{index}开始");

                var req = new PageBaseReq();
                req.PageNum = index;
                req.PageSize = 1000;
                req.Sort = "ID";

                var lstOld = QueryFromYY1(req);
                if (lstOld.Count == 0)
                {
                    return;
                }

                var lstNew = new List<YY1>();
                foreach (var old in lstOld)
                {
                    var newItem = new YY1();
                    lstNew.Add(newItem);

                    newItem.ID = 0;
                    newItem.Title = EncryptHelper.EncryptAES(old.Title);
                    newItem.Url = EncryptHelper.EncryptAES(old.Url);
                    newItem.CType = EncryptHelper.EncryptAES(old.CType);
                    newItem.Content = EncryptHelper.EncryptAES(old.Content);
                }

                SaveToYY(lstNew);

                Console.WriteLine($"{index}完成");
            });
        }

        private static List<YY1> QueryFromYY1(PageBaseReq req)
        {
            using (var db = new YY1Context())
            {
                var q = db.YY1s.AsNoTracking();
                q = q.OrderBy(m => m.ID);

                return QueryPageList<YY1>(q, req);
            }
        }

        private static int SaveToYY(List<YY1> list)
        { 
            using (var db = new YYContext())
            {
                db.YY1s.AddRange(list);
                var ret = db.SaveChanges();

                return ret;
            }
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="q"></param>
        /// <param name="req">条件</param>
        /// <returns></returns>
        private static PageList<T> QueryPageList<T>(IQueryable<T> q, PageBaseReq req) where T : new()
        {
            //总条数
            var totalRecord = 0;

            //是否需要分页
            if (req.IsSetPage())
            {
                totalRecord = q.Count();
            }

            //设置排序和分页
            SetSortPage(ref q, req);

            //查询列表
            var list = q.ToList();

            //是否需要分页
            if (req.IsSetPage() == false)
            {
                totalRecord = list.Count;
            }

            return new PageList<T>(list, totalRecord);
        }

        /// <summary>
        /// 设置排序和分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="q"></param>
        /// <param name="req">条件</param>
        private static void SetSortPage<T>(ref IQueryable<T> q, PageBaseReq req)
        {
            //排序
            if (string.IsNullOrWhiteSpace(req.Sort) == false)
            {
                q = q.OrderBy(req.Sort); 
            }

            //分页
            if (req.IsSetPage())
            {
                q = q.Skip(req.GetSkip()).Take(req.PageSize);
            }
        }
    }
}
